from pyecharts.charts import Page
import pandas as pd
import sys
sys.path.append("..//database")
from SqlWizard import *
from CmnFuncs import *

def run_other_chems():
    '''
    本模块包含品种：天然橡胶，20号胶，纸浆， 尿素，关联品种：MA，动力煤，玉米
    设计图表：
    1. 20号胶主连图；
    2. nr / ru
    3. 天然橡胶现货基差图；
    4. 天然橡胶月差表；
    5. 纸浆现货基差图；
    6. 纸浆月差图；
    7. 尿素现货基差表；
    8. 尿素月差表；
    9. 尿素 / MA；
    10. 尿素 / 动力煤；
    11. 尿素 / 玉米；
    '''

    a_list = date_checker_1()

    nr_main = read_sql_fut('fut_nrl')

    ru_spot = read_sql_spot('spot_ru')
    ru_index = read_sql_fut_index('fut_index_ru')
    ru_ry = read_sql_ry('roll_yield_ru')
    ru_spdrdx = read_sql_spdrdx('spdrdex_ru')
    ru_1 = read_sql_fut('fut_ru{}'.format(a_list[0]))
    ru_2 = read_sql_fut('fut_ru{}'.format(a_list[1]))
    ru_3 = read_sql_fut('fut_ru{}'.format(a_list[2]))

    sp_spot = read_sql_spot('spot_sp')
    sp_index = read_sql_fut_index('fut_index_sp')
    sp_ry = read_sql_ry('roll_yield_sp')
    sp_spdrdx = read_sql_spdrdx('spdrdex_sp')
    sp_1 = read_sql_fut('fut_sp{}'.format(a_list[0]))
    sp_2 = read_sql_fut('fut_sp{}'.format(a_list[1]))
    sp_3 = read_sql_fut('fut_sp{}'.format(a_list[2]))

    ur_spot = read_sql_spot('spot_ur')
    ur_index = read_sql_fut_index('fut_index_ur')
    ur_ry = read_sql_ry('roll_yield_ur')
    ur_spdrdx = read_sql_spdrdx('spdrdex_ur')
    ur_1 = read_sql_fut('fut_ur{}'.format(a_list[0]))
    ur_2 = read_sql_fut('fut_ur{}'.format(a_list[1]))
    ur_3 = read_sql_fut('fut_ur{}'.format(a_list[2]))

    ma_spot = read_sql_spot('spot_ma')
    ma_index = read_sql_fut_index('fut_index_ma')
    ma_1 = read_sql_fut('fut_ma{}'.format(a_list[0]))
    ma_2 = read_sql_fut('fut_ma{}'.format(a_list[1]))
    ma_3 = read_sql_fut('fut_ma{}'.format(a_list[2]))

    zc_spot = read_sql_spot('spot_zc')
    zc_index = read_sql_fut_index('fut_index_zc')
    zc_1 = read_sql_fut('fut_zc{}'.format(a_list[0]))
    zc_2 = read_sql_fut('fut_zc{}'.format(a_list[1]))
    zc_3 = read_sql_fut('fut_zc{}'.format(a_list[2]))

    c_spot = read_sql_spot('spot_c')
    c_index = read_sql_fut_index('fut_index_c')
    c_1 = read_sql_fut('fut_c{}'.format(a_list[0]))
    c_2 = read_sql_fut('fut_c{}'.format(a_list[1]))
    c_3 = read_sql_fut('fut_c{}'.format(a_list[2]))

    sa_spot = read_sql_spot('spot_sa')
    sa_index = read_sql_fut_index('fut_index_sa')
    sa_ry = read_sql_ry('roll_yield_sa')
    sa_spdrdx = read_sql_spdrdx('spdrdex_sa')
    sa_1 = read_sql_fut('fut_sa{}'.format(a_list[0]))
    sa_2 = read_sql_fut('fut_sa{}'.format(a_list[1]))
    sa_3 = read_sql_fut('fut_sa{}'.format(a_list[2]))

    fg_spot = read_sql_spot('spot_fg')
    fg_index = read_sql_fut_index('fut_index_fg')
    fg_a1 = read_sql_fut('fut_fg{}'.format(a_list[0]))
    fg_a2 = read_sql_fut('fut_fg{}'.format(a_list[1]))
    fg_a3 = read_sql_fut('fut_fg{}'.format(a_list[2]))

    # 20号胶
    header_nr = draw_separator('20号胶')
    # 1. 20号胶主连图；
    nr_main_chart = draw_main('20号胶', nr_main['close'])

    # 天胶
    header_ru = draw_separator('天然橡胶')
    # 天胶远期曲线
    future_curve_ru = draw_future_curve(ru_spot, ru_1, ru_2, ru_3)
    # 天胶一致性预期指标
    idctr_ru = draw_spec_idctr('RU', ru_spdrdx, ru_index, ru_ry)
    # 3. 天然橡胶现货基差图；
    ru_spot_chart = draw_spot_spd('RU', ru_spot)
    # 天胶合约价格、成交量、持仓量、持仓量变化图
    ru_contract_1 = draw_contract('ru{}'.format(a_list[0]), ru_1)
    ru_contract_2 = draw_contract('ru{}'.format(a_list[1]), ru_2)
    ru_contract_3 = draw_contract('ru{}'.format(a_list[2]), ru_3)
    # 4. 天然橡胶月差表；
    ru_12_chart = draw_spd('ru{}'.format(a_list[0]), ru_1['close'], 'ru{}'.format(a_list[1]), ru_2['close'])
    ru_23_chart = draw_spd('ru{}'.format(a_list[1]), ru_2['close'], 'ru{}'.format(a_list[2]), ru_3['close'])

    # 纸浆
    header_sp = draw_separator('纸浆')
    # 纸浆远期曲线
    future_curve_sp = draw_future_curve(sp_spot, sp_1, sp_2, sp_3)
    # 纸浆一致性预期指标
    idctr_sp = draw_spec_idctr('纸浆', sp_spdrdx, sp_index, sp_ry)
    # 5. 纸浆现货基差图；
    sp_spot_chart = draw_spot_spd('SP', sp_spot)
    # 纸浆合约价格、成交量、持仓量、持仓量变化图
    sp_contract_1 = draw_contract('sp{}'.format(a_list[0]), sp_1)
    sp_contract_2 = draw_contract('sp{}'.format(a_list[1]), sp_2)
    sp_contract_3 = draw_contract('sp{}'.format(a_list[2]), sp_3)
    # 6. 纸浆月差表；
    sp_12_chart = draw_spd('sp{}'.format(a_list[0]), sp_1['close'], 'sp{}'.format(a_list[1]), sp_2['close'])
    sp_23_chart = draw_spd('sp{}'.format(a_list[1]), sp_2['close'], 'sp{}'.format(a_list[2]), sp_3['close'])

    # 尿素
    header_ur = draw_separator('尿素')
    # 尿素远期曲线
    future_curve_ur = draw_future_curve(ur_spot, ur_1, ur_2, ur_3)
    # ur一致性预期指标
    idctr_ur = draw_spec_idctr('UR', ur_spdrdx, ur_index, ur_ry)
    # 7. 尿素现货基差表；
    ur_spot_chart = draw_spot_spd('UR', ur_spot)
    # 尿素合约价格、成交量、持仓量、持仓量变化图
    ur_contract_1 = draw_contract('ur{}'.format(a_list[0]), ur_1)
    ur_contract_2 = draw_contract('ur{}'.format(a_list[1]), ur_2)
    ur_contract_3 = draw_contract('ur{}'.format(a_list[2]), ur_3)
    # 8. 尿素月差表；
    ur_12_chart = draw_spd('ur{}'.format(a_list[0]), ur_1['close'], 'ur{}'.format(a_list[1]), ur_2['close'])
    ur_23_chart = draw_spd('ur{}'.format(a_list[1]), ur_2['close'], 'ur{}'.format(a_list[2]), ur_3['close'])

    # 纯碱
    header_sa = draw_separator('纯碱')
    # 纯碱远期曲线
    future_curve_sa = draw_future_curve(sa_spot, sa_1, sa_2, sa_3)
    # 纯碱一致性预期指标
    idctr_sa = draw_spec_idctr('SA', sa_spdrdx, sa_index, sa_ry)
    # 7. 纯碱现货基差表；
    sa_spot_chart = draw_spot_spd('SA', sa_spot)
    # 纯碱合约价格、成交量、持仓量、持仓量变化图
    sa_contract_1 = draw_contract('sa{}'.format(a_list[0]), sa_1)
    sa_contract_2 = draw_contract('sa{}'.format(a_list[1]), sa_2)
    sa_contract_3 = draw_contract('sa{}'.format(a_list[2]), sa_3)
    # 8. 纯碱月差表；
    sa_12_chart = draw_spd('sa{}'.format(a_list[0]), sa_1['close'], 'sa{}'.format(a_list[1]), sa_2['close'])
    sa_23_chart = draw_spd('sa{}'.format(a_list[1]), sa_2['close'], 'sa{}'.format(a_list[2]), sa_3['close'])

    # 品种间
    header_cross = draw_separator('品种间')
    # 9. 尿素 / MA；
    arbi_record('ur', ur_index['close'], 'ma', ma_index['close'], x=1, y=1, opt=1, cat=1)
    arbi_record('ur', ur_spot['spot'], 'ma', ma_spot['spot'], x=1, y=1, opt=1, cat=2)
    ratio_ur_ma_spot = draw_price_ratio('ur现货', ur_spot['spot'], 'ma现货', ma_spot['spot'], 1)
    ratio_ur_ma_main = draw_price_ratio('ur主连', ur_spot['main_price'], 'ma主连', ma_spot['main_price'], 1)
    ratio_ur_ma_1 = draw_price_ratio('ur{}'.format(a_list[0]), ur_1['close'], 'ma{}'.format(a_list[0]), ma_1['close'], 1)
    ratio_ur_ma_2 = draw_price_ratio('ur{}'.format(a_list[1]), ur_2['close'], 'ma{}'.format(a_list[1]), ma_2['close'], 1)
    ratio_ur_ma_3 = draw_price_ratio('ur{}'.format(a_list[2]), ur_3['close'], 'ma{}'.format(a_list[2]), ma_3['close'], 1)
    # 10. 尿素 / 动力煤；
    arbi_record('ur', ur_index['close'], 'zc', zc_index['close'], x=1, y=1, opt=1, cat=1)
    arbi_record('ur', ur_spot['spot'], 'zc', zc_spot['spot'], x=1, y=1, opt=1, cat=2)
    ratio_ur_zc_spot = draw_price_ratio('ur现货', ur_spot['spot'], 'zc现货', zc_spot['spot'])
    ratio_ur_zc_main = draw_price_ratio('ur主连', ur_spot['main_price'], 'zc主连', zc_spot['main_price'])
    ratio_ur_zc_1 = draw_price_ratio('ur{}'.format(a_list[0]), ur_1['close'], 'zc{}'.format(a_list[0]), zc_1['close'])
    ratio_ur_zc_2 = draw_price_ratio('ur{}'.format(a_list[1]), ur_2['close'], 'zc{}'.format(a_list[1]), zc_2['close'])
    ratio_ur_zc_3 = draw_price_ratio('ur{}'.format(a_list[2]), ur_3['close'], 'zc{}'.format(a_list[2]), zc_3['close'])
    # 11. 尿素 / 玉米；
    arbi_record('ur', ur_index['close'], 'c', c_index['close'], x=1, y=1, opt=1, cat=1)
    arbi_record('ur', ur_spot['spot'], 'c', c_spot['spot'], x=1, y=1, opt=1, cat=2)
    ratio_ur_c_spot = draw_price_ratio('ur现货', ur_spot['spot'], 'c现货', c_spot['spot'], 3)
    ratio_ur_c_main = draw_price_ratio('ur主连', ur_spot['main_price'], 'c主连', c_spot['main_price'], 3)
    ratio_ur_c_1 = draw_price_ratio('ur{}'.format(a_list[0]), ur_1['close'], 'c{}'.format(a_list[0]), c_1['close'], 3)
    ratio_ur_c_2 = draw_price_ratio('ur{}'.format(a_list[1]), ur_2['close'], 'c{}'.format(a_list[1]), c_2['close'], 3)
    ratio_ur_c_3 = draw_price_ratio('ur{}'.format(a_list[2]), ur_3['close'], 'c{}'.format(a_list[2]), c_3['close'], 3)
    # 纯碱 / 玻璃；
    arbi_record('sa', sa_index['close'], 'fg', fg_index['close'], x=1, y=1, opt=1, cat=1)
    arbi_record('sa', sa_spot['spot'], 'fg', fg_spot['spot'], x=1, y=1, opt=1, cat=2)
    ratio_sa_fg_spot = draw_price_ratio('sa现货', sa_spot['spot'], 'fg现货', fg_spot['spot'], 2)
    ratio_sa_fg_main = draw_price_ratio('sa主连', sa_spot['main_price'], 'fg主连', fg_spot['main_price'], 2)
    ratio_sa_fg_1 = draw_price_ratio('sa{}'.format(a_list[0]), sa_1['close'], 'fg{}'.format(a_list[0]), fg_a1['close'], 2)
    ratio_sa_fg_2 = draw_price_ratio('sa{}'.format(a_list[1]), sa_2['close'], 'fg{}'.format(a_list[1]), fg_a2['close'], 2)
    ratio_sa_fg_3 = draw_price_ratio('sa{}'.format(a_list[2]), sa_3['close'], 'fg{}'.format(a_list[2]), fg_a3['close'], 2)
    # 2. 20号胶主连 / 天然橡胶主力
    ratio_nr_ru = draw_price_ratio('20号胶主连', nr_main['close'], 'ru{}'.format(a_list[0]), ru_1['close'], 2)

    page = Page(page_title='其他化工品', layout=Page.SimplePageLayout)
    page.add(
        header_nr,
        nr_main_chart,
        header_ru,
        future_curve_ru,
        idctr_ru,
        ru_spot_chart,
        ru_contract_1,
        ru_contract_2,
        ru_contract_3,
        ru_12_chart,
        ru_23_chart,
        header_sp,
        future_curve_sp,
        idctr_sp,
        sp_spot_chart,
        sp_contract_1,
        sp_contract_2,
        sp_contract_3,
        sp_12_chart,
        sp_23_chart,
        header_ur,
        future_curve_ur,
        idctr_ur,
        ur_spot_chart,
        ur_contract_1,
        ur_contract_2,
        ur_contract_3,
        ur_12_chart,
        ur_23_chart,
        header_sa,
        future_curve_sa,
        idctr_sa,
        sa_spot_chart,
        sa_contract_1,
        sa_contract_2,
        sa_contract_3,
        sa_12_chart,
        sa_23_chart,
        header_cross,
        ratio_ur_ma_spot,
        ratio_ur_ma_main,
        ratio_ur_ma_1,
        ratio_ur_ma_2,
        ratio_ur_ma_3,
        ratio_ur_zc_spot,
        ratio_ur_zc_main,
        ratio_ur_zc_1,
        ratio_ur_zc_2,
        ratio_ur_zc_3,
        ratio_ur_c_spot,
        ratio_ur_c_main,
        ratio_ur_c_1,
        ratio_ur_c_2,
        ratio_ur_c_3,
        ratio_sa_fg_spot,
        ratio_sa_fg_main,
        ratio_sa_fg_1,
        ratio_sa_fg_2,
        ratio_sa_fg_3,
        ratio_nr_ru
    )
    page.render('其他化工品.html')
